package com.webridge.plm.mvc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import wt.fc.QueryResult;
import wt.part.WTPart;
import wt.part.WTPartHelper;
import wt.part.WTPartMaster;
import wt.part.WTPartUsageLink;
import wt.query.QueryException;
import wt.query.QuerySpec;
import wt.session.SessionServerHelper;
import wt.util.WTException;
import wt.vc.config.ConfigSpec;
import wt.vc.config.LatestConfigSpec;
import wt.vc.struct.StructHelper;

import com.ptc.core.components.beans.TreeHandlerAdapter;
import com.webridge.plm.util.PartUtil;
/**
 * http://dev.x1929.com/Windchill/netmarkets/jsp/com/webridge/utiltest/generateEBOMStep.jsp?oid=VR:wt.part.WTPart:148277
 * 
 * @author kira
 */
public class EBOMTreeHandler extends TreeHandlerAdapter {
    public static Logger LOGGER = Logger.getLogger(EBOMTreeHandler.class.getName());
    private WTPart part = null;
    public EBOMTreeHandler(WTPart part) {
        this.part = part;
    }

    /**
     * Get the root node from the command bean, and get a config spec based on
     * the root node
     **/
    public List getRootNodes() throws WTException {
        List list = new ArrayList();
        list.add(part);

        return list;
    }

    /**
     * Get the child parents for the given list of parent parts
     */
    public Map<Object, List> getNodes(List parents)  {
        Map<Object, List> result = new HashMap<Object, List>();

        boolean enforce = SessionServerHelper.manager.setAccessEnforced(false);
        try {
            for (Object object : parents) {
                
                if (object instanceof WTPart) {
                    List children = new ArrayList();
                    WTPart tmpPart = (WTPart) object;
                    LOGGER.debug("object WTPart:" + tmpPart);

                    QuerySpec qs = new QuerySpec(WTPartUsageLink.class);
                    LatestConfigSpec spec = new LatestConfigSpec();
                    QueryResult linkQr = StructHelper.service.navigateUses(tmpPart, false);
                    LOGGER.debug("object linkQr:" + linkQr.size());

                    while (linkQr.hasMoreElements()) {
                        Object childEPM = (Object) linkQr.nextElement();
                        if (childEPM instanceof WTPartUsageLink) {
                            WTPartUsageLink childLink = (WTPartUsageLink) childEPM;
                            LOGGER.debug("object childLink:" + childLink+",childLink:" + childLink.getRoleBObject());
                            children.add(childLink);
                        }
                    }
                    result.put(object, children);
                }else if(object instanceof WTPartUsageLink){
                    List children = new ArrayList();
                    WTPartUsageLink link = (WTPartUsageLink) object;
                    LOGGER.debug("object WTPartUsageLink:" + link);

                    WTPartMaster master = (WTPartMaster) link.getRoleBObject();
                    WTPart tmpPart = PartUtil.getLatestPartByMaster(master);
                    QuerySpec qs = new QuerySpec(WTPartUsageLink.class);
                    LatestConfigSpec spec = new LatestConfigSpec();
                    QueryResult linkQr = StructHelper.service.navigateUses(tmpPart, false);
                    while (linkQr.hasMoreElements()) {
                        Object childEPM = (Object) linkQr.nextElement();
                        if (childEPM instanceof WTPartUsageLink) {
                            WTPartUsageLink childLink = (WTPartUsageLink) childEPM;
                            children.add(childLink);
                        }
                    }
                    result.put(object, children);
                }
            }
        } catch (QueryException e) {
            e.printStackTrace();
        } catch (WTException e) {
            e.printStackTrace();
        }finally{
            SessionServerHelper.manager.setAccessEnforced(enforce);

        }

        return result;
    }


}
